package init;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import objects.AccessPoint;
import objects.Point;

import dao.AccessPointsDAO;
import divTools.CheckMAC;

public class InitAps {
	
	/**
	 * imports the list of access points from a config file
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static int addAps(String path) throws IOException {
		
		int nbAps = 0;
		
		try
	    {
	       BufferedReader buffer = new BufferedReader(new FileReader(path));
	       String line;
	       ArrayList<AccessPoint> Aps = new ArrayList<AccessPoint>();

	       while((line = buffer.readLine())!= null)
	       {
	    	   if (!line.startsWith("#") && !line.isEmpty()) {
	    		   String[] splittedLine = line.split(";");
	    		   
	    		   AccessPoint newAp = new AccessPoint(); 
	    		   
	    		   try {
	    			   if (!CheckMAC.IsMAC(splittedLine[0]))
		    			   continue;
		    		   newAp.setMacAdress(splittedLine[0].toUpperCase());
	    			   Point newPoint = new Point(Float.parseFloat(splittedLine[1]), Float.parseFloat(splittedLine[2]), Float.parseFloat(splittedLine[3]));
	    			   newAp.setCoordinates(newPoint);
	    			   newAp.setFrequency(Long.parseLong(splittedLine[4]));
	    			   newAp.setAntennaGain(Float.parseFloat(splittedLine[5]));
	    			   newAp.setOutputPower(Float.parseFloat(splittedLine[6]));	  
	    			   Aps.add(newAp);
	    		   }
	    		   catch (NumberFormatException e)
	    		   {
	    			   continue;
	    		   }
	    	   }
	       }
	       buffer.close();	
	       
	       nbAps = AccessPointsDAO.create(Aps);
	    }
	    catch (FileNotFoundException e)
	    {
	       System.out.println("Le fichier est introuvable !");
	    }
	    
	    return nbAps;
	}
}
